package com.group.dao;

import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import com.group.model.BrainQuestion;
import com.group.model.News;
import com.group.model.User;
import com.group.model.UserTicker;

@Repository
public interface UserDao {

	public List<User> queryUser(@Param("key") String key, @Param("type") String type, @Param("start") Integer start, @Param("limit") Integer limit,@Param("date") String date);

	public int queryUserCount(@Param("key") String key,@Param("type") String type,@Param("date") String date);

	@Update("update `user` u set u.followCount = (select count(*) from userfollow f where f.userId = #{arg0}) where u.id = #{arg0}")
	public void updateUserFollow(Long userId);

	@Update("update `user` u set u.fansCount = (select count(*) from userfollow f where f.followUserId = #{arg0}) where u.id = #{arg0}")
	public void updateUserFans(Long userId);

	@Update("update `user` u set u.topicCount = (select count(*) from topic t where t.userId = #{arg0} and t.state = 1) where u.id = #{arg0}")
	public void updateUserTopicCount(Long userId);

	@Update("update `user` u set u.topicReplyCount = (select count(distinct t.topicId) from topicreply t where t.userId = #{userId} and t.state = 1) where u.id = #{userId}")
	public void updateUserTopicReplyCount(@Param("userId") Long userId);

	public List<User> queryUserFollow(@Param("userId") Long userId, @Param("start") Integer start, @Param("limit") Integer limit);

	public int queryUserFollowCount(@Param("userId") Long userId);

	public List<User> queryUserFans(@Param("userId") Long userId, @Param("start") Integer start, @Param("limit") Integer limit);

	public int queryUserFollowFansCount(@Param("userId") Long userId);

	public List<News> queryUserNews(@Param("userId") Long userId, @Param("start") Integer start, @Param("limit") Integer limit);

	public int queryUserNewsCount(@Param("userId") Long userId);

	@Select("select count(*) from news n where n.userId =#{arg0} and n.state = 1")
	public int queryUserPublishNewsCount(Long userId);

	@Select("select count(*) from activitylog l where l.userId = #{arg0} and l.activityDateId in (${activityDateIds})")
	public int countSign(Long userId, @Param("activityDateIds") String activityDateIds);

	@Select("SELECT * FROM brainquestion ORDER BY RAND() LIMIT 5")
	public List<BrainQuestion> getBrainQuestions();
	
	@Select("SELECT * FROM BrainQuestion1 ORDER BY RAND() LIMIT 5")
	public List<BrainQuestion> getBrainQuestions1();
	
	@Select("SELECT * FROM `user` WHERE id >= ((SELECT MAX(id) FROM `user`)-(SELECT MIN(id) FROM `user`)) * RAND() + (SELECT MIN(id) FROM `user`)  LIMIT 1")
	public User getRandomUser();
	
	@Select("select count(*) from BrainResult r where r.userId = #{userId} and r.date=#{date}")
	public int countBrainGames(@Param("userId") Long userId, @Param("date") String date);
	
	@Select("select count(*) from BrainResult r where r.userId = #{userId} and r.date=#{date} and r.result = '胜利'")
	public int countWinBrainGames(@Param("userId") Long userId, @Param("date") String date);
	
	@Select("select count(*) from BrainGameShare r where r.userId = #{userId} and r.date=#{date}")
	public int countBrainShare(@Param("userId") Long userId, @Param("date") String date);
	
	public List<UserTicker> queryUserTicker(@Param("userId") Long userId, @Param("cid") String cid);

	@Select("SELECT * FROM USER AS u WHERE u.`freezeStatus`=#{arg0} LIMIT #{arg1},#{arg2}")
	List<User> queryFreezeUser(Integer freezeStatus, Integer start, Integer limit);

	@Select("select count(1) from user where freezeStatus=#{arg0}")
	int queryFreezeCount(Integer freezeStatus);
}
